package ca.compsci.opent.doctool;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility parses source code and extracts code constructs
 * 
 * @since 0.0.3
 */
public class SourceCodeParser
{
	/**
	 * List of source code lines
	 * 
	 * @since 0.0.3
	 */
	private List<String> src;
	
	/**
	 * List of variables
	 * 
	 * @since 0.0.3
	 */
	private List<Var> vars;
	
	/**
	 * List of constants
	 * 
	 * @since 0.0.3
	 */
	private List<Const> consts;
	
	/**
	 * List of procedures
	 * 
	 * @since 0.0.3
	 */
	private List<Procedure> procs;
	
	/**
	 * List of functions
	 * 
	 * @since 0.0.3
	 */
	private List<Function> funcs;
	
    /**
     * Creates a new {@code SourceCodeParser} to parse the specified code.
	 * 
	 * @param src list of lines in source code
	 * 
	 * @since 0.0.3
     */
    public SourceCodeParser(List<String> src)
    {
		this.src = src;
		
		vars = new ArrayList<Var>();
		consts = new ArrayList<Const>();
		procs = new ArrayList<Procedure>();
		funcs = new ArrayList<Function>();
    }
	
	/**
	 * Parse the source code.
	 *
	 * @return if the parsing was succesful
	 * 
	 * @throws OpenTDocException when a parsing error has occured
	 * 
	 * @since 0.0.3
	 */
	public boolean parse() throws OpenTDocException
	{
		for (String s : src)
		{
			String[] tokens = s.split("\\s");
			
			if (tokens[0].equals("var"))
			{
				Type type = getType(tokens);
				for (String i : getIdentifierTokens(tokens))
				{
					vars.add(new Var(i, type));
				}
			}
			else if (tokens[0].equals("const"))
			{
				Type type = getType(tokens);
				String value = getValue(tokens);
				for (String i : getIdentifierTokens(tokens))
				{
					consts.add(new Const(i, type, value));
				}
			}
		}
		
		return true;
	}
	
	private Var[] readVars(String[] tokens)
	{
		List<String> identifiers = new ArrayList<String>(tokens.length);
		
		
		for (String s : getIdentifierTokens(tokens))
		{
			
		}
		
		return null;
	}
	
	private List<String> getIdentifierTokens(String[] tokens)
	{
		List<String> identifiers = new ArrayList<String>(tokens.length);
		for (String t : tokens)
		{
			if (t.equals(":")) break;

			identifiers.add(t.replace(",", ""));
		}
		
		return identifiers;
	}
	
	private Type getType(String[] tokens) throws OpenTDocException
	{
		int i = 0;
		while (i < tokens.length && !tokens[i].equals(":"))
			i++; // Just finding where type is, past :
		
		return Type.getType(tokens[i]);
	}
	
	private String getValue(String[] tokens)
	{
		int i = 0;
		while (i < tokens.length && !tokens[i].equals(":="))
			i++; // Just finding where value is, past :=
		
		return tokens[i];
	}
	
	/**
	 * Return the list of source lines
	 * 
	 * @return src list
	 * 
	 * @since 0.0.3
	 */
	public List<String> getSrcList()
	{
		return src;
	}
	
	/**
	 * Return the list of vars
	 * 
	 * @return var list
	 * 
	 * @since 0.0.3
	 */
	public List<Var> getVarList()
	{
		return vars;
	}
	
	/**
	 * Return the list of consts
	 * 
	 * @return const list
	 * 
	 * @since 0.0.3
	 */
	public List<Const> geConstList()
	{
		return consts;
	}
	
	/**
	 * Return the list of procedures
	 * 
	 * @return procedure list
	 * 
	 * @since 0.0.3
	 */
	public List<Procedure> getProcedureList()
	{
		return procs;
	}
	
	/**
	 * Return the list of functions
	 * 
	 * @return function list
	 * 
	 * @since 0.0.3
	 */
	public List<Function> getFunctionList()
	{
		return funcs;
	}
}
